import os
import json
from unit.models.dimensionModel import *
from unit.models.unitModel import *
from unit.models.quantityModel import *
from basic_component_related_management.models.componentGroup import *
from basic_component_related_management.models.componentAttribute import *
from basic_component_related_management.models import *


meta_data_dir = "./files/thermoml_related_dataV1/meta_data"

# 初始化一个单位集合


def init_unit_data():
    unit_data_path = os.path.join(meta_data_dir, "unit.json")
    with open(unit_data_path, "r", encoding="utf-8") as f:
        unit_data = json.load(f)
    dimension_name_id = unit_data["dimension_name_id"]
    dimension_less_entity = Dimension.objects.filter(
        dimension_id=dimension_name_id).first()
    basic_unit = Unit.objects.filter(
        dimension=dimension_less_entity, is_system=True).first()
    thermoml_units = unit_data["unit"]

    user_name = "admin"
    unit_list = []
    unit_symbol_list = []
    unit_conversion_list = []
    for thermoml_unit in thermoml_units:
        # 构造单位
        unit_item = {}
        unit_item["unit_name"] = "{}(thermoml)".format(thermoml_unit)
        unit_item["unit_name_en"] = "{}(thermoml)".format(thermoml_unit)
        unit_item["symbol"] = "{}(thermoml)".format(thermoml_unit)
        unit_item["dimension"] = dimension_less_entity
        unit_item["comment"] = "thermoml系统初始化"
        unit_item["create_person"] = user_name
        unit_item["modify_person"] = user_name
        unit = Unit(**unit_item)
        unit_list.append(unit)
        unit_symbol_item = {}
        unit_symbol_item["symbol"] = "{}(thermoml)".format(thermoml_unit)
        unit_symbol_item["root_unit"] = unit
        unit_symbol_item["comment"] = "系统创建单位符号(thermoml)"
        unit_symbol_item["create_person"] = user_name
        unit_symbol_item["modify_person"] = user_name
        unit_symbol_item["is_system"] = True
        unit_symbol = UnitSymbol(**unit_symbol_item)
        unit_symbol_list.append(unit_symbol)
        # 构建单位转换关系
        unit_conversion_item = {}
        unit_conversion_item["base_unit"] = basic_unit
        unit_conversion_item["target_unit"] = unit
        unit_conversion_item["factor"] = 1.0
        unit_conversion_item["offset"] = 0.0
        unit_conversion_item["create_person"] = user_name
        unit_conversion_item["modify_person"] = user_name
        unit_conversion = UnitConversion(**unit_conversion_item)
        unit_conversion_list.append(unit_conversion)

    # 构建量
    quantity_item = {}
    quantity_item["quantity_name"] = "thermoml量"
    quantity_item["quantity_name_en"] = "thermoml quantity"
    quantity_item["quantity_type"] = "NORMAL"
    quantity_item["dimension"] = dimension_less_entity
    quantity_item["comment"] = "thermoml系统初始化"
    quantity_item["create_person"] = user_name
    quantity_item["modify_person"] = user_name
    quantity = Quantity(**quantity_item)
    quantity_unit_list = []
    for unit in unit_list:
        quantity_unit_list.append(
            QuantityUnitMap(quantity=quantity, unit=unit))

    Unit.objects.bulk_create(unit_list)
    UnitSymbol.objects.bulk_create(unit_symbol_list)
    UnitConversion.objects.bulk_create(unit_conversion_list)
    Quantity.objects.bulk_create([quantity])
    QuantityUnitMap.objects.bulk_create(quantity_unit_list)

    print("init_unit_data Done!")


# 初始化组分类别和组分属性
def init_group_category_and_component_property():
    component_attribute_data_path = os.path.join(
        meta_data_dir, "component_attribute_data.json")
    with open(component_attribute_data_path, "r") as f:
        component_attribute_data = json.load(f)
    datatype_map_path = os.path.join(meta_data_dir, "datatype_map.json")
    with open(datatype_map_path, "r") as f:
        datatype_map = json.load(f)
    user_name = "admin"
    component_group_list = []
    component_attribute_list = []
    component_group_attribute_list = []
    for component_attribute in component_attribute_data:

        component_group_item = {}
        component_group_item["name"] = "{}(thermoml)".format(
            component_attribute["attribute_group"])
        component_group_item["name_en"] = "{}(thermoml)".format(
            component_attribute["attribute_group"])
        component_group_item["create_person"] = user_name
        component_group_item["modify_person"] = user_name
        component_group = ComponentGroup(**component_group_item)
        component_group_list.append(component_group)
        for attribute in component_attribute["attributes"]:
            # 增加属性
            attribute_item = {}
            attribute_item["main_name"] = "{}(thermoml)".format(
                attribute["name"])
            attribute_item["main_name_en"] = "{}(thermoml)".format(
                attribute["name"])
            attribute_item["main_dataIndex"] = "{}(thermoml)".format(
                attribute["name"])
            attribute_item["main_datatype"] = datatype_map[attribute["type"]]
            attribute_item["main_necessary"] = False
            attribute_item["description"] = "{}\n{}".format(
                attribute["appinfo"], attribute["documentation"])
            attribute_item["multiple_value_used"] = attribute["multiple"]
            attribute_item["is_system_config"] = True
            attribute_item["create_person"] = user_name
            attribute_item["modify_person"] = user_name
            attribute1 = Attribute(**attribute_item)
            component_attribute_list.append(attribute1)
            # 增加组分类别和属性关系
            component_group_attribute_list.append(AttributeComGroupMap(
                componentgroup=component_group, attribute=attribute1))
    ComponentGroup.objects.bulk_create(component_group_list)
    Attribute.objects.bulk_create(component_attribute_list)
    AttributeComGroupMap.objects.bulk_create(component_group_attribute_list)

    print("init_group_category_and_component_property Done!")


def init_component_property():
    property_group_property_method_data_path = os.path.join(
        meta_data_dir, "property_group_property_method.json")
    with open(property_group_property_method_data_path, "r") as f:
        property_group_property_method_data = json.load(f)
    user_name = "admin"

    quantity_entity = Quantity.objects.filter(
        quantity_name="thermoml量").first()

    property_group_list = []
    property_list = []
    property_method_list = []
    property_method_dict = {}
    property_property_method_list = []
    property_method_set = set()
    num = 0
    for property_group_property_method in property_group_property_method_data:
        property_method_set = set()
        property_group_item = {}
        property_group_item["name"] = "{}(thermoml)".format(
            property_group_property_method["property_group"])
        property_group_item["name_en"] = "{}(thermoml)".format(
            property_group_property_method["property_group"])
        property_group_item["create_person"] = user_name
        property_group_item["modify_person"] = user_name
        property_group = PropertyGroup(**property_group_item)
        property_group_list.append(property_group)

        propertys = property_group_property_method["property"]
        property_list_1 = []
        for property in propertys:
            proprety_item = {}
            proprety_item["name"] = "{}(thermoml)".format(property["property"])
            proprety_item["name_en"] = "{}(thermoml)".format(
                property["property"])
            proprety_item["common_used"] = True
            proprety_item["property_group"] = property_group
            proprety_item["quantity"] = quantity_entity
            proprety_item["create_person"] = user_name
            proprety_item["modify_person"] = user_name
            property1 = Property(**proprety_item)
            property_list.append(property1)
            property_list_1.append(property1)
        methods = property_group_property_method["method"]
        property_method_list_1 = []
        for method in methods:
            method_item = {}
            method_item["name"] = "{}(thermoml)".format(method)
            method_item["name_en"] = "{}(thermoml)".format(method)
            method_item["type"] = "experiment"
            method_item["create_person"] = user_name
            method_item["modify_person"] = user_name

            if method_item["name"] in property_method_dict:
                property_method_list_1.append(
                    property_method_dict[method_item["name"]])

            else:
                property_method = Method(**method_item)
                property_method_dict[method_item["name"]] = property_method
                property_method_list.append(property_method)
                property_method_list_1.append(property_method)

        for property in property_list_1:
            for method in property_method_list_1:

                property_property_method_list.append(
                    MethodPropertyMap(method=method, property=property))

    PropertyGroup.objects.bulk_create(property_group_list)
    Property.objects.bulk_create(property_list)
    Method.objects.bulk_create(property_method_list)
    MethodPropertyMap.objects.bulk_create(property_property_method_list)

    print("init_component_property Done!")

def init_phase():
    phase_data_path = os.path.join(meta_data_dir, "phase.json")
    with open(phase_data_path, "r") as f:
        phase_data = json.load(f)
    user_name = "admin"
    phase_list = []
    for phase in phase_data:
        phase_item = {}
        phase_item["name"] = "{}(thermoml)".format(phase)
        phase_item["name_en"] = "{}(thermoml)".format(phase)
        phase_item["create_person"] = user_name
        phase_item["modify_person"] = user_name
        phase1 = Phase(**phase_item)
        phase_list.append(phase1)
    Phase.objects.bulk_create(phase_list)
    
    print("init_phase Done!")
    
def init_purification():
    purifMethod_data_path = os.path.join(meta_data_dir, "purifMethod.json")
    with open(purifMethod_data_path, "r") as f:
        purifMethod_data = json.load(f)
    user_name = "admin"
    purifMethod_list = []
    for purifMethod in purifMethod_data:
        purifMethod_item = {}
        purifMethod_item["name"] = "{}(thermoml)".format(purifMethod)
        purifMethod_item["name_en"] = "{}(thermoml)".format(purifMethod)
        purifMethod_item["create_person"] = user_name
        purifMethod_item["modify_person"] = user_name
        purifMethod1 = Purification(**purifMethod_item)
        purifMethod_list.append(purifMethod1)
    Purification.objects.bulk_create(purifMethod_list)
    
    print("init_purification Done!")
    
def init_purityDetermination():
    purityDetermination_data_path = os.path.join(meta_data_dir, "ePurityAnalMethod.json")
    with open(purityDetermination_data_path, "r") as f:
        purityDetermination_data = json.load(f)
    user_name = "admin"
    purityDetermination_list = []
    for purityDetermination in purityDetermination_data:
        purityDetermination_item = {}
        purityDetermination_item["name"] = "{}(thermoml)".format(purityDetermination)
        purityDetermination_item["name_en"] = "{}(thermoml)".format(purityDetermination)
        purityDetermination_item["create_person"] = user_name
        purityDetermination_item["modify_person"] = user_name
        purityDetermination1 = PurityDetermination(**purityDetermination_item)
        purityDetermination_list.append(purityDetermination1)
    PurityDetermination.objects.bulk_create(purityDetermination_list)
    
    print("init_purityDetermination Done!")



def init_thermoml_dataV1():
    init_unit_data()
    init_group_category_and_component_property()
    init_component_property()
    init_phase()
    init_purification()
    init_purityDetermination()
    
    print("init_thermoml_dataV1 Done!")
